//
//  CityListTableCell.swift
//  旅航ios
//
//  Created by tulip on 2021/5/19.
//

import UIKit
import SwiftyJSON
class CityListTableCell: UITableViewCell {
    
    var closeView:(()->Void)?
    
    var isDepart:Bool!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        self.selectionStyle = .none
        // Configure the view for the selected state
    }
    
    func setBtn(n:Int,dic:Array<JSON>){
        
        let btn_width=Int((SCREEN_WIDTH-30-18)/4.0)
        for i in 0...n-1{
            let btn=UIButton.init(frame: CGRect(x: 15+i%4*(btn_width+6), y: 20+i/4*(37+7), width: btn_width, height: 37))
            btn.backgroundColor=UIColor.init(hexString: "f5f3f2")
            btn.setTitle(dic[i]["city"].string ?? "", for: .normal)
            btn.setTitleColor(UIColor.init(hexString: "333333"), for: .normal)
            btn.layer.cornerRadius=3
            btn.mtag=dic[i]["code"].string ?? ""
            btn.addTarget(self, action: #selector(chooseCity), for: .touchUpInside)
            self.addSubview(btn)
        }
    }
    
    
    @objc func chooseCity(sender:UIButton){
        let dic=["cityName":sender.titleLabel?.text,"cityCode":sender.mtag]
       
        if isDepart {
            
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "chooseDepart"), object: dic, userInfo: nil)
            
            
        }else{
            
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "chooseArrive"), object: dic, userInfo: nil)
           

        }
        
        closeView!()
        
    }
    
}
